package com.mallcloud.mall.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallcloud.mall.common.core.constant.enums.app.PointsProductRedeemEnum;
import com.mallcloud.mall.mall.api.entity.MallPointsInRecordModel;
import com.mallcloud.mall.mall.api.entity.MallPointsOutRecordModel;
import com.mallcloud.mall.mall.api.entity.MallPointsProductRedeemRecordModel;
import com.mallcloud.mall.mall.api.entity.vo.MallPointsProductRedeemRecordVO;
import com.mallcloud.mall.mall.api.entity.vo.MallPointsRedeemRecordExcelVO;
import com.mallcloud.mall.mall.mapper.MallPointsProductRedeemRecordMapper;
import com.mallcloud.mall.mall.service.MallPointsProductRedeemRecordService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 积分商品兑换记录
 */
@Service
public class MallPointsProductRedeemRecordServiceImpl extends ServiceImpl<MallPointsProductRedeemRecordMapper, MallPointsProductRedeemRecordModel> implements MallPointsProductRedeemRecordService {

	@Override
	public Boolean draw(List<Long> ids) {
		if (Objects.isNull(ids) || ids.isEmpty()) {
			return Boolean.TRUE;
		}
		this.updateBatchById(
				ids.stream().map(x -> {
					MallPointsProductRedeemRecordModel model = new MallPointsProductRedeemRecordModel();
					model.setId(x);
					model.setRedeemStatus(PointsProductRedeemEnum.RedeemStatusEnum.Y.name());
					return model;
				})
				.collect(Collectors.toList())
		);
		return Boolean.TRUE;
	}

	@Override
	public IPage<MallPointsProductRedeemRecordVO> pagePointsRedeemRecord(Page page, MallPointsProductRedeemRecordModel dto) {
		IPage<MallPointsProductRedeemRecordVO> pageResult = this.baseMapper.pagePointsProductRedeemRecord(page, dto);
		pageResult.getRecords().forEach(x ->
				Optional.ofNullable(x.getRedeemStatus())
						.filter(StrUtil::isNotBlank)
						.map(PointsProductRedeemEnum.RedeemStatusEnum::getDecs)
						.filter(StrUtil::isNotBlank)
						.ifPresent(x::setRedeemStatusName)
		);
		return pageResult;
	}

	@Override
	public List<MallPointsProductRedeemRecordVO> listPointsRedeemRecord(MallPointsProductRedeemRecordModel dto) {
		List<MallPointsProductRedeemRecordVO> result = this.baseMapper.pagePointsProductRedeemRecord(dto);
		result.forEach(x ->
				Optional.ofNullable(x.getRedeemStatus())
						.filter(StrUtil::isNotBlank)
						.map(PointsProductRedeemEnum.RedeemStatusEnum::getDecs)
						.filter(StrUtil::isNotBlank)
						.ifPresent(x::setRedeemStatusName)
		);
		return result;
	}

	@Override
	public List<MallPointsRedeemRecordExcelVO> export(MallPointsProductRedeemRecordModel dto) {
		List<MallPointsProductRedeemRecordVO> result = this.baseMapper.pagePointsProductRedeemRecord(dto);
		List<MallPointsRedeemRecordExcelVO> resultExcel = BeanUtil.copyToList(result, MallPointsRedeemRecordExcelVO.class);
		resultExcel.forEach(x ->
				Optional.ofNullable(x.getRedeemStatus())
						.filter(StrUtil::isNotBlank)
						.map(PointsProductRedeemEnum.RedeemStatusEnum::getDecs)
						.filter(StrUtil::isNotBlank)
						.ifPresent(x::setRedeemStatusName)
		);
		return resultExcel;
	}

	@Override
	public IPage<MallPointsInRecordModel> pagePointsRedeemInRecord(Page page, MallPointsInRecordModel model) {
		return null;
	}

	@Override
	public IPage<MallPointsOutRecordModel> pagePointsRedeemOutRecord(Page page, MallPointsOutRecordModel model) {
		return null;
	}
}
